package com.example.netlib.loading;

import android.app.Activity;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;
import androidx.databinding.DataBindingUtil;

import com.example.netlib.R;
import com.example.netlib.databinding.DialogProgressBinding;
import com.szlanyou.thirdsdk.retrofit.loading.ILoading;
import com.szlanyou.thirdsdk.retrofit.util.MyLogUtil;

import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * QQ 1511895018
 * Created by 谈泳豪 on 2018/6/1.
 */
public class LoadingDialog extends AppCompatDialog implements ILoading {
    private static final String TAG = "LoadingDialog";
    private boolean isShowing = false;
    private  long showTime;
    private  int numOfDialog;
    private final ProgressDialogViewModel mViewModel;
    private WeakReference<Activity> activityWeakReference;
    private final DialogProgressBinding mBinding;
    private long lastClickTimeMilliSecs = 0;

    public LoadingDialog(Activity activity) {
        super(activity, R.style.loadingDialogStyle);
        activityWeakReference = new WeakReference<>(activity);
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.dialog_progress, null, false);
        mViewModel = new ProgressDialogViewModel();
        mBinding.setModel(mViewModel);

        setContentView(mBinding.getRoot());
        //一定要在setContentView之后调用，否则无效
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        setCanceledOnTouchOutside(false);
        setCancelable(false);

        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

        setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                isShowing = false;
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                isShowing = false;
            }
        });
        mBinding.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //double click
                if(lastClickTimeMilliSecs != 0){
                    if(System.currentTimeMillis() - lastClickTimeMilliSecs < 300){
                        hideLoading();
                    }
                }
                lastClickTimeMilliSecs = System.currentTimeMillis();
            }
        });
        isShowing = false;
        setAnimation();
    }

    private void setAnimation() {

    }

    @Override
    public void show() {
        Log.d(TAG,"show numOfDialog = " + numOfDialog + ",isShowing = " + isShowing + ",activity = " + activityWeakReference.get().getClass().getName());
        if(!isShowing) {
            mBinding.lottieView.playAnimation();
            showTime = System.currentTimeMillis();
            numOfDialog = 0;
            isShowing = true;
            super.show();
        }else{
            numOfDialog++;
        }
        lastClickTimeMilliSecs = 0;
    }

    @Override
    public void dismiss() {
        Log.d(TAG,"dismiss numOfDialog = " + numOfDialog + ",isShowing = " + isShowing + ",activity = " + activityWeakReference.get().getClass().getName());
        if(numOfDialog==0) {
            mBinding.lottieView.cancelAnimation();
            super.dismiss();
        }else{
            numOfDialog--;
        }
        lastClickTimeMilliSecs = 0;
    }

    @Override
    public boolean onKeyUp(int keyCode,@NonNull KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            return false;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    public void showLoading(String loadingTip) {
        if(null == loadingTip){
            showLoading();
            return;
        }
        mViewModel.setTitle(loadingTip);
        show();
    }

    @Override
    public void showLoading() {
        mViewModel.setTitle("请求中...");
        show();
    }

    @Override
    public void hideLoading() {
        dismiss();
       /* if(activityWeakReference != null && activityWeakReference.get() != null){
            //Android中View not attached to window manager错误的解决办法
            if(activityWeakReference.get().isFinishing()){
                return;
            }
        }*/
    }

    @Override
    public void showFailure(Throwable e) {

    }

    @Override
    public void showFailure(Throwable e, String failureTip) {

    }

    @Override
    public void releaseLoading() {

    }

}
